#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:吹神
@file:operating_yaml.py
@time:2021/05/06
"""

import yaml
import os
from config.settings import log


def read_yaml(file):
    """
    读取yaml文件用例
    :param file: 文件路径
    :return: 返回两个列表，一个包含测试用例描述，一个包含测试数据
    """
    # 判断文件是否存在
    if os.path.isfile(file):
        with open(file, encoding="utf-8") as f:
            dic = yaml.safe_load(f)
        data_list = []
        for key, value in dic.items():
            doc = value.pop('doc', "无描述信息")
            data_list.append((doc, value))
        return data_list
    else:
        raise FileNotFoundError(f"【{file}】-文件不存在")


def get_one_yaml(file, key):
    """
    获取单个用例
    :param file: 文件路径
    :param key: 用例名称
    :return:
    """
    if os.path.isfile(file):
        with open(file, encoding="utf-8") as f:
            dic = yaml.safe_load(f)
        doc = dic.pop("doce", "")
        return doc, dic.get(key, "")


def write_yaml(file, data):
    """
    写入yaml文件
    :param file:
    :param data:
    :return:
    """
    with open(file, encoding="utf-8") as f:
        yaml.dump(f, data)
        log.info(f"写入{file}-内容成功")
        return True


if __name__ == '__main__':
    doc, data = read_yaml(r"D:\CaseProject\PyDemo\test_case\data\user.yaml")
    print(doc)
    print(data)
